package com.jzr.medical.common;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class LayServiceImpl<M extends LayMapper<T,V,K>,T extends LayEntity<K>,V extends T,K extends Serializable>
        extends ServiceImpl<M,T>
        implements ILayService<T,V,K>{

    @Override
    public V getByIdV(Serializable id){
        return baseMapper.getById(id);
    }
    @Override
    public List<V> list(IPage<V> page,QueryWrapper<V> wrapper){
        return baseMapper.list(page,wrapper);
    }
    @Override
    public List<V> list(QueryWrapper<V> wrapper){
        return this.list(null,wrapper);
    }
    @Override
    public V getOne(QueryWrapper<V> wrapper){
        List<V> list = this.list(wrapper);
        if(list != null && list.size()>0){
            return list.get(0);
        }
        return null;
    }
    @Override
    public List<V> listByIdsV(Collection<? extends Serializable> idList){
        QueryWrapper<V> qw = new QueryWrapper<>();
        if(idList != null && idList.size()>0) {
            qw.in("id", idList);
        }
        return list(qw);
    }

    @Override
    public List<V> listByMap(IPage<V> page,Map<String, Object> columnMap){
        QueryWrapper<V> qw = new QueryWrapper<>();
        if(columnMap != null && columnMap.size()>0) {
            Set<String> keys = columnMap.keySet();
            for (String k : keys) {
                qw.eq(k, columnMap.get(k));
            }
        }
        return list(page,qw);
    }
    @Override
    public List<V> listByMapV(Map<String, Object> columnMap){
        IPage<V> page = null;
        return listByMap(page,columnMap);
    }
    @Override
    public K getId(Wrapper wrapper){
        List<K> list = baseMapper.getIdList(wrapper);
        if(list != null && list.size()>0){
            return list.get(0);
        }
        return null;
    }
    @Override
    public List<K> getIdList(Wrapper wrapper){
        return baseMapper.getIdList(wrapper);
    }

    public void setDefaultValue(T entity){
        if(entity != null){
            if(entity.getUpdateBy() == null){
                entity.setUpdateBy(SecurityHelper.getUserId());
            }
            if(entity.getUpdateTime() == null){
                entity.setUpdateTime(LocalDateTime.now());
            }
        }
    }
    public void setDefaultValue(Collection<T> entityList){
        Integer userId = SecurityHelper.getUserId();
        for(T entity : entityList) {
            if (entity != null) {
                if (entity.getUpdateBy() == null) {
                    entity.setUpdateBy(userId);
                }
                if (entity.getUpdateTime() == null) {
                    entity.setUpdateTime(LocalDateTime.now());
                }
            }
        }
    }
    @Override
    public boolean save(T entity) {
        setDefaultValue(entity);
        return super.save(entity);
    }

    @Override
    public boolean saveOrUpdate(T entity) {
        setDefaultValue(entity);
        return super.saveOrUpdate(entity);
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
        setDefaultValue(entityList);
        return super.saveOrUpdateBatch(entityList, batchSize);
    }

    @Override
    public boolean saveBatch(Collection<T> entityList, int batchSize) {
        setDefaultValue(entityList);
        return super.saveBatch(entityList, batchSize);
    }

    @Override
    public boolean updateById(T entity) {
        setDefaultValue(entity);
        return super.updateById(entity);
    }

    @Override
    public boolean update(T entity, Wrapper<T> updateWrapper) {
        setDefaultValue(entity);
        return super.update(entity, updateWrapper);
    }

    @Override
    public boolean updateBatchById(Collection<T> entityList, int batchSize) {
        setDefaultValue(entityList);
        return super.updateBatchById(entityList, batchSize);
    }
}
